package com.smfx.yxt.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionSynchronizationAdapter;
import org.springframework.transaction.support.TransactionSynchronizationManager;

import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.FunctionRoomStatus;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.common.enums.TeacherHomeStatus;
import com.smfx.yxt.common.enums.WorkApprovalType;
import com.smfx.yxt.daoSqlserver.TeacherHomeMapper;
import com.smfx.yxt.daoSqlserver.TeacherInfoMapper;
import com.smfx.yxt.daoSqlserver.WorkApprovalConfMapper;
import com.smfx.yxt.model.TeacherHome;
import com.smfx.yxt.model.TeacherInfo;
import com.smfx.yxt.model.WorkApprovalConf;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.service.ITeacherHomeService;
import com.smfx.yxt.utils.PropertiesUtil;
import com.smfx.yxt.view.TeacherHomeView;
import com.smfx.yxt.wechat.utils.TemplateUtil;

@Service("teacherHomeService")
public class TeacherHomeServiceImpl implements ITeacherHomeService {

	@Autowired
	private TeacherHomeMapper teacherHomeMapper;
	@Autowired
	private TeacherInfoMapper teacherInfoMapper;
	@Autowired
	private WorkApprovalConfMapper workApprovalConfMapper;
	
	@Override
	public int insertSelective(TeacherHome record) {
		return teacherHomeMapper.insertSelective(record);
	}

	@Override
	public RspResult addAndUpdate(TeacherHome record) {
		if(record.getId() == null) {
			teacherHomeMapper.insertSelective(record);
		}else {
			TeacherHome baseResult = teacherHomeMapper.selectByPrimaryKey(record.getId());
			if(TeacherHomeStatus.CANCEL.getCode().equals(baseResult.getStatus())) {
				return new RspResult(Status.PARAM_FAIL.getCode(), "该建议已被申请人撤消");
			}else if(TeacherHomeStatus.REPLYED.getCode().equals(baseResult.getStatus())) {
				return new RspResult(Status.PARAM_FAIL.getCode(), "该建议已被回复");
			}
			record.setUpdateTime(new Date());
			teacherHomeMapper.updateByPrimaryKeySelective(record);
		}
		TeacherHomeThreadExecute(record);
		return new RspResult(Status.SUCCESS);
	}

	@Override
	public List<TeacherHomeView> selectListByPage(Page<TeacherHomeView> page) {
		return teacherHomeMapper.selectListByPage(page);
	}

	void TeacherHomeThreadExecute(TeacherHome apply) {
		TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronizationAdapter() {
	        @Override
	        public void afterCommit() {
	        	String reqUrl = Const.BASEPATH + Const.TEACHER_ROLE + 
						MenuConst.TEACHER_HOME + "/detail/" + apply.getId();
	        	String approvalType = "教师之家";
	        	if(FunctionRoomStatus.PENDING.getCode().equals(apply.getStatus())) {
	        		String templateId = PropertiesUtil.getProperty("work_approval_apply_template");
	        		WorkApprovalConf conf = workApprovalConfMapper.selectByItemCode(
	        				WorkApprovalType.ROOM_APPROVAL.getCode());
					List<TeacherInfo> teacherList = teacherInfoMapper.selectListByIds(
							Arrays.asList(conf.getApprovalerId().split(",")));
					for(TeacherInfo teacher : teacherList) {
						TemplateUtil.approvalApplySend(teacher.getOpenid2(), approvalType, apply.getTeacherName(), 
								apply.getProblem(), new Date(), templateId, reqUrl);
					}
	        	}else if(FunctionRoomStatus.AGREE.getCode().equals(apply.getStatus()) || 
	        			FunctionRoomStatus.REJECT.getCode().equals(apply.getStatus()) ||
	        			FunctionRoomStatus.APPROVER_CANCEL.getCode().equals(apply.getStatus())) {
	        		String templateId = PropertiesUtil.getProperty("work_approval_result_template");
	        		String title = "您好！您提交的功能室申请已经被审批";
	        		if(FunctionRoomStatus.APPROVER_CANCEL.getCode().equals(apply.getStatus())) {
	        			title = "您好！您申请到的功能室被审批人取消，请知悉";
	        		}
	        		TeacherInfo applyTeacher = teacherInfoMapper.selectByPrimaryKey(apply.getTeacherId());
					TemplateUtil.approvalResultSend(applyTeacher.getOpenid2(), title, approvalType, 
							FunctionRoomStatus.getValue(apply.getStatus()), new Date(), templateId, reqUrl);
	        	}
	        }
	    });
	}
}
